/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) 2010 Roman Divotkey

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.cogaen.demo.entity;

import org.cogaen.box2d.PhysicsService;
import org.cogaen.core.Core;
import org.cogaen.entity.Entity;
import org.cogaen.name.NameService;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;

public class SquareEntity extends Entity {

	public static final String TYPE = "Square";
	public static final Vec2 SIZE = new Vec2(2.0f, 2.0f);
	public static final float DENSITY = 1.0f;

	private Vec2 pos = new Vec2(0.0f, 0.0f);
	private Body body;
			
	public SquareEntity(Core core, String name, double x, double y) {
		super(core, name);
		pos.x = (float) x;
		pos.y = (float) y;
	}

	public SquareEntity(Core core, double x, double y) {
		this(core, NameService.getInstance(core).generateName(), x, y);
	}
	
	protected Body getBody() {
		return this.body;
	}
		
	@Override
	protected void tearDown() {
		World world = PhysicsService.getInstance( getCore() ).getWorld();
		world.destroyBody( this.body );
	}

	@Override
	protected void setUp() {
		World world = PhysicsService.getInstance( getCore() ).getWorld();
		
		BodyDef def = new BodyDef();
		def.position.x = this.pos.x;
		def.position.y = this.pos.y;
		def.linearDamping = 0.0f;		
		def.angularDamping = 0.0f;
		def.userData = getName();
		
		this.body = world.createBody(def);
		
		PolygonDef polyDef = new PolygonDef();
		polyDef.setAsBox(SIZE.x / 2.0f, SIZE.y / 2.0f);
		polyDef.density = DENSITY;
		polyDef.restitution = 0.4f;
		
		this.body.createShape( polyDef );
		this.body.setMassFromShapes();
	}

	@Override
	public String getType() {
		return TYPE;
	}

	@Override
	public void update() {
		// intentionally left empty
	}

}
